package com.jxl.service;

import com.jxl.mapper.UserMapper;
import com.jxl.pojo.User;
import com.jxl.pojo.UserLoginRecord;
import com.jxl.utils.IPUtils;
import com.jxl.utils.Md5Utils;
import com.jxl.utils.WebUtils;
import com.jxl.web.WebResult;
import com.jxl.web.constant.Constant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 用户业务
 * @author 江雪林
 * @create 2021-03-29
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserLoginRecordService userLoginRecordService;

    /**
     * 用户登录
     * @param req
     * @param username
     * @param password
     * @return
     */
    public boolean login(HttpServletRequest req, HttpServletResponse resp, String username, String password) throws IOException {
        PrintWriter out = resp.getWriter();
        WebResult webResult = new WebResult();
        if(username != null || password != null){
            User user = userMapper.login(username, password);
            if(user != null){
                req.getSession().setAttribute(Constant.SESSION_USER,user);
                webResult.setCode(Constant.SUCCESS);
                webResult.setMessage(Constant.LOGIN_SUCCESS);
                //WebUtils.response(resp,webResult);
                //添加用户登录记录
                UserLoginRecord record = new UserLoginRecord(user.getId(), IPUtils.getIpAddr(req));
                //如果这里添加失败，需要使用消息队列来实现，先不做处理
                System.out.println("添加用户登录信息是否成功："+userLoginRecordService.addRecord(record));
                return true;
            }
        }
        webResult.setCode(Constant.UNSUCCESS);
        webResult.setMessage(Constant.LOGIN_UNSUCCESS);
        WebUtils.response(resp,webResult);
        return false;
    }

    /**
     * 用户名是否存在
     * @param username
     * @return
     */
    public boolean UserNameIsExsist(String username){
        if(username != null){
            return userMapper.findCountByUserName(username)>0;
        }
        return false;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    public boolean register(HttpServletResponse resp,User user) throws IOException {
        WebResult webResult = new WebResult(Constant.UNSUCCESS);
        if(user!=null){
            if(user.getUsername()!=null && user.getPassword()!=null){
                if(!this.UserNameIsExsist(user.getUsername())){
                    //设置默认权限
                    user.setRole("dev");
                    //模拟前端对密码进行加密
                    user.setPassword(Md5Utils.MD5Lower(user.getPassword()));
                    if(userMapper.register(user)>0){
                        webResult.setCode(Constant.SUCCESS);
                        webResult.setMessage(Constant.REGISTER_SUCCESS);
                        //WebUtils.response(resp,webResult);
                        return true;
                    }
                }else{
                    webResult.setMessage(Constant.USER_EXSIST);
                    //WebUtils.response(resp,webResult);
                    return false;
                }
            }
        }
        webResult.setMessage(Constant.REGISTER_UNSUCCESS);
        //WebUtils.response(resp,webResult);
        return false;
    }
}
